package handler

import (
	"github.com/gin-gonic/gin"
	"log"
	"onerk4/zg5yk/ordering/api/client"
	"onerk4/zg5yk/ordering/api/enit/request"
	"onerk4/zg5yk/ordering/api/enit/response"
	"onerk4/zg5yk/ordering/common/pkg"
	"onerk4/zg5yk/ordering/common/proto/user"
)

func Login(c *gin.Context) {
	var req request.UserLogin

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}

	login, err := client.UserLogin(c, &user.UserLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func ReceiveCoupon(c *gin.Context) {
	var req request.ReceiveCoupon

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.ReceiveCoupon(c, &user.ReceiveCouponRequest{
		UserId:   int64(userId),
		CouponId: req.CouponId,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func ShowCoupon(c *gin.Context) {
	var req request.ShowCoupon

	if err := c.ShouldBind(&req); err != nil {
		response.ErrorResponse(c, "参数错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	login, err := client.ShowCoupon(c, &user.ShowCouponRequest{
		UserId: int64(userId),
		Status: req.Status,
	})
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}

	response.SuccessResponse(c, "ok", login)
}

func Upload(c *gin.Context) {
	// 单文件
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "../upload/" + file.Filename
	// 上传文件至指定的完整文件路径
	err := c.SaveUploadedFile(file, dst)
	if err != nil {
		response.ErrorResponse(c, "err", err.Error())
		return
	}
	
	upload := pkg.BigFileUpload(dst, file.Filename)

	response.SuccessResponse(c, "ok", upload)
}
